package com.yanqu.road.dao.impl.sacrifice;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.sacrifice.UserSacrifice;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserSacrificeDaoImpl extends TempDataObjectDao<UserSacrifice> {

    public UserSacrifice getUserSacrifice(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_sacrifice where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSacrifice data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserSacrifice data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_sacrifice(userId,modifyTime,totalKillBossTimes,todayDamage,abilityRecordMark) values (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getTotalKillBossTimes());
            call.setString(i++, data.getTodayDamage().toString());
            call.setLong(i++, data.getAbilityRecordMark());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserSacrifice data) {
        PreparedStatement call = null;
        String sql = "update t_u_sacrifice set modifyTime=?,totalKillBossTimes=?,todayDamage=?,abilityRecordMark=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getTotalKillBossTimes());
            call.setString(i++, data.getTodayDamage().toString());
            call.setLong(i++, data.getAbilityRecordMark());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserSacrifice parseData(ResultSet rs) throws SQLException {
        UserSacrifice data = new UserSacrifice();
        data.setUserId(rs.getLong("userId"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setTotalKillBossTimes(rs.getLong("totalKillBossTimes"));
        String value = rs.getString("todayDamage");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTodayDamage(new BigInteger(value));
        }
        data.setAbilityRecordMark(rs.getLong("abilityRecordMark"));
        return data;
    }
}
